﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using YogaStudio.Data;
using YogaStudio.Data.Domain;
using YogaStudio.Service.BLL;

namespace YogaStudio.WebUI.Controllers
{
    public class CustomersAdminController : Controller
    {
   
        //private YogaStudioEntities db = new YogaStudioEntities();

        //// GET: CustomersAdmin
        //public ActionResult Index()
        //{
        //    var customers = db.Customers.Include(c => c.Advisor).Include(c => c.User);
        //    return View(customers.ToList());
        //}

        //// GET: CustomersAdmin/Details/5
        //public ActionResult Details(int? id)
        //{
        //    if (id == null)
        //    {
        //        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        //    }
        //    Customer customer = db.Customers.Find(id);
        //    if (customer == null)
        //    {
        //        return HttpNotFound();
        //    }
        //    return View(customer);
        //}

        //// GET: CustomersAdmin/Create
        //public ActionResult Create()
        //{
        //    ViewBag.FacultyId = new SelectList(db.Faculties, "Id", "Title");
        //    ViewBag.UserId = new SelectList(db.Users, "UserId", "UserName");
        //    return View();
        //}

        //// POST: CustomersAdmin/Create
        //// To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        //// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        //[HttpPost]
        //[ValidateAntiForgeryToken]
        //public ActionResult Create([Bind(Include = "Id,UserId,FullName,Address,PhoneNumber,FacultyId")] Customer customer)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        db.Customers.Add(customer);
        //        db.SaveChanges();
        //        return RedirectToAction("Index");
        //    }

        //    ViewBag.FacultyId = new SelectList(db.Faculties, "Id", "Title", customer.FacultyId);
        //    ViewBag.UserId = new SelectList(db.Users, "UserId", "UserName", customer.UserId);
        //    return View(customer);
        //}

        //// GET: CustomersAdmin/Edit/5
        //public ActionResult Edit(int? id)
        //{
        //    if (id == null)
        //    {
        //        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        //    }
        //    Customer customer = db.Customers.Find(id);
        //    if (customer == null)
        //    {
        //        return HttpNotFound();
        //    }
        //    ViewBag.FacultyId = new SelectList(db.Faculties, "Id", "Title", customer.FacultyId);
        //    ViewBag.UserId = new SelectList(db.Users, "UserId", "UserName", customer.UserId);
        //    return View(customer);
        //}

        //// POST: CustomersAdmin/Edit/5
        //// To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        //// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        //[HttpPost]
        //[ValidateAntiForgeryToken]
        //public ActionResult Edit([Bind(Include = "Id,UserId,FullName,Address,PhoneNumber,FacultyId")] Customer customer)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        db.Entry(customer).State = EntityState.Modified;
        //        db.SaveChanges();
        //        return RedirectToAction("Index");
        //    }
        //    ViewBag.FacultyId = new SelectList(db.Faculties, "Id", "Title", customer.FacultyId);
        //    ViewBag.UserId = new SelectList(db.Users, "UserId", "UserName", customer.UserId);
        //    return View(customer);
        //}

        //// GET: CustomersAdmin/Delete/5
        //public ActionResult Delete(int? id)
        //{
        //    if (id == null)
        //    {
        //        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        //    }
        //    Customer customer = db.Customers.Find(id);
        //    if (customer == null)
        //    {
        //        return HttpNotFound();
        //    }
        //    return View(customer);
        //}

        //// POST: CustomersAdmin/Delete/5
        //[HttpPost, ActionName("Delete")]
        //[ValidateAntiForgeryToken]
        //public ActionResult DeleteConfirmed(int id)
        //{
        //    Customer customer = db.Customers.Find(id);
        //    db.Customers.Remove(customer);
        //    db.SaveChanges();
        //    return RedirectToAction("Index");
        //}

        //protected override void Dispose(bool disposing)
        //{
        //    if (disposing)
        //    {
        //        db.Dispose();
        //    }
        //    base.Dispose(disposing);
        //}

        //private YogaStudioEntities db = new YogaStudioEntities();
        CustomerService customerService = new CustomerService();
        FacultySerivce facutyService = new FacultySerivce();
        User user;
        UserManagementService userManagement = new UserManagementService();
        // GET: Customers
        public ActionResult Index()
        {
            //var customers = db.Customers.Include(c => c.Faculty).Include(c => c.User);
            return View(customerService.GetAllCustomers());
        }

        // GET: Customers/Details/5
        public ActionResult Details()
        {
            user = userManagement.GetUser(User.Identity.Name);
            if (user == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Customer customer = customerService.GetCustomerByUserId(user.UserId);
            if (customer == null)
            {
                return HttpNotFound();
            }
            return View(customer);
        }

        // GET: Customers/Create
        public ActionResult Create()
        {
            ViewBag.FacultyId = new SelectList(facutyService.GetAllFaculties(), "Id", "FullName");
            //ViewBag.UserId = new SelectList(db.Users, "UserId", "UserName");
            user = userManagement.GetUser(User.Identity.Name);
            if (user != null)
            {
                Customer cust = customerService.GetCustomerByUserId(user.UserId);
                if (cust != null)
                {
                    return View(cust);
                }
            }
            return View();
        }

        // POST: Customers/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,FullName,Address,PhoneNumber")] Customer customer)
        {
            // if (ModelState.IsValid)
            // {
            user = userManagement.GetUser(User.Identity.Name);
            if (user != null)
            {
                customer.UserId = user.UserId;
                Customer cust = customerService.GetCustomerByUserId(user.UserId);
                if (cust == null)
                {

                    customerService.SaveCustomer(customer);
                }
                else
                {
                    customerService.UpdateCusotmer(customer);
                }
            }

            // return RedirectToAction("Details");

            return View("Details");
        }

        // GET: Customers/Edit/5
        public ActionResult Edit()
        {
            user = userManagement.GetUser(User.Identity.Name);
            if (user == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Customer customer = customerService.GetCustomerByUserId(user.UserId);
            if (customer == null)
            {
                return HttpNotFound();
            }
             ViewBag.FacultyId = new SelectList(facutyService.GetAllFaculties(), "Id", "FullName", customer.FacultyId);
            // ViewBag.UserId = new SelectList(db.Users, "UserId", "UserName", customer.UserId);
            return View(customer);
        }

        // POST: Customers/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "Id,FullName,Address,PhoneNumber, FacultyId")] Customer customer)
        {
            Customer cust = customerService.GetCustomer(customer.Id);
     
            customer.UserId = cust.UserId;
            customerService.UpdateCusotmer(customer);
            return RedirectToAction("Index");
            
       
        }

        // GET: Customers/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Customer customer = customerService.GetCustomer((int)id);
            if (customer == null)
            {
                return HttpNotFound();
            }
            return View(customer);
        }

        // POST: Customers/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Customer customer = customerService.GetCustomer((int)id);
            customerService.DeleteCustomer(customer);
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                customerService.Dispose(true);
            }
            base.Dispose(disposing);
        }
    }
}
